<?php

namespace app\admin\model\department;

use think\Model;

/**
 * 模型
 */
class ArticleFile extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = false;


    protected static function init()
    {
        self::beforeInsert(function ($row) {
            $tempArr = explode("/", $row['file']);
            if (!empty($tempArr[3])) {
                $tempArr = explode(".", $tempArr[3]);
                $row['file_name'] = $tempArr[0];
            }
        });


        self::beforeUpdate(function ($row) {
            $changedata = $row->getChangedData();
            if (isset($changedata['file'])) {
                $tempArr = explode("/", $row['file']);
                if (!empty($tempArr[3])) {
                    $tempArr = explode(".", $tempArr[3]);
                    $row['file_name'] = $tempArr[0];
                }
            }
        });
    }


    public function article()
    {
        return $this->belongsTo('Article', 'article_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function user()
    {
        return $this->belongsTo('app\admin\model\Admin', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


}
